package com.pinda.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pinda.users.entity.PdMember;
import com.pinda.service.IPdMemberService;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.lang.reflect.Member;
import java.util.UUID;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-member")
public class PdMemberController {
    @Resource
    private IPdMemberService iPdMemberService;

    @PostMapping("login")
    public PdMember login(@RequestBody PdMember pdMember) throws Exception {

        return this.iPdMemberService.queryMember(pdMember);
    }

    @PostMapping("saveMember")
    public boolean saveMember(@RequestBody PdMember pdMember){
        return this.iPdMemberService.saveMember(pdMember);
    }

    @GetMapping("getCode")
    public int getCode(String phone){
        return this.iPdMemberService.getCode(phone);
    }

    @PostMapping("updateMember")
    public boolean updateMember(@RequestBody PdMember pdMember){
        return this.iPdMemberService.updateById(pdMember);
    }

    @PostMapping("uploadcontroller")
    public String uploadcontroller(@RequestBody() MultipartFile myfile) {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tBDtxb5zU2extkovwUW";
        String accessKeySecret = "oFZi4U5KRMtq1kZy8igFLCXcOD9l2U";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "projectvue";
        //你上传到oss后的名字 会根据日期帮你创建文件夹。
        String objectName = String.valueOf(UUID.randomUUID());
        String hz = myfile.getOriginalFilename().substring(myfile.getOriginalFilename().indexOf(".") + 1);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            InputStream inputStream = myfile.getInputStream();
            // 创建PutObject请求。
            ossClient.putObject(bucketName, objectName + "." + hz, inputStream);
        } catch (Exception oe) {

        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        //https://qy151.oss-cn-hangzhou.aliyuncs.com/2022/6/10/20d3d7e6b5bb455cb548675501f7270fgdnj.jpg
        String url = "https://" + bucketName + "." + endpoint + "/" + objectName + "." + hz;
        return url;
    }

}
